/*
 * @Author: xzy 1453077527@qq.com
 * @Date: 2025-08-25 14:05:13
 * @LastEditors: xzy 1453077527@qq.com
 * @LastEditTime: 2025-08-26 21:00:57
 * @Description: 用户业务层
 */
package com.xtedu.examproject.service;

import com.xtedu.examproject.dao.base.RoleDetailInfoDao;
import com.xtedu.examproject.dao.base.UserDao;
import com.xtedu.examproject.dao.impl.StudentDaoImpl;
import com.xtedu.examproject.dao.impl.TeacherDaoImpl;
import com.xtedu.examproject.dao.impl.UserDaoImpl;
import com.xtedu.examproject.entity.User;
import com.xtedu.examproject.util.PageHelper;

import java.util.List;

public class UserService {
    private UserDao userDao;
    private RoleDetailInfoDao roleDetailInfoDao;

    public UserService() {
        userDao = new UserDaoImpl();
        roleDetailInfoDao = new UserDaoImpl();
    }

    public int insertUser(User user) throws Exception {
        return userDao.insert(user);
    }

    public int deleteUser(User user) throws Exception {
        return userDao.delete(user);
    }

    public int deleteUserById(int id) throws Exception {
        return userDao.deleteById(id);
    }

    public int updateUser(User user) throws Exception {
        return userDao.update(user);
    }

    public int updateUserState(int userId, int state) {
        return userDao.updateUserState(userId, state);
    }

    public int updateUserRole(int userId, int roleId) {
        return userDao.updateUserRole(userId, roleId);
    }

    public User findUserById(int id) throws Exception {
        return userDao.findById(id);
    }

    public List<User> findByRoleId(int roleId) throws Exception {
        return userDao.findByRoleId(roleId);
    }

    public List<User> findAllUser() throws Exception {
        return userDao.findAll();
    }

    public User findUserByUsername(String username) throws Exception {
        return userDao.findByUsername(username);
    }

    /**
     * 根据角色id查询用户
     *
     * @param pageIndex 页码
     * @param pageSize  每页显示数量
     * @param roleIds   角色id
     * @return 用户列表
     */
    public PageHelper findPageByRoleIds(int pageIndex, int pageSize, int... roleIds) throws Exception {
        return userDao.findPageByRoleIds(pageIndex, pageSize, roleIds);
    }

    public PageHelper findPageByRoleId(int roleId, int userId, int pageIndex, int pageSize, int roleselect) throws Exception {
        return userDao.findPageByRoleId(roleId, userId, pageIndex, pageSize, roleselect);
    }

    /**
     * 显示用户详情信息
     *
     * @param userId 用户id
     * @param roleId 角色id
     * @param <T>    泛型 返回可能为学生，也可能为老师
     * @return 用户详情信息
     */
    public <T> T showDetailInfo(int userId, int roleId) throws Exception {

        if (roleId == 1) {
            roleDetailInfoDao = new TeacherDaoImpl();
            return (T) roleDetailInfoDao.showDetailInfo(userId);
        }
        if (roleId == 4) {
            roleDetailInfoDao = new StudentDaoImpl();
            return (T) roleDetailInfoDao.showDetailInfo(userId);
        }
        return null;
    }

    /**
     * 分页查询用户
     *
     * @param pageIndex 页码
     * @param pageSize  每页显示数量
     * @return 用户分页对象
     */
    public PageHelper findAllUser(int pageIndex, int pageSize) throws Exception {
        return userDao.findAllByPage(pageIndex, pageSize);
    }

    /**
     * 根据用户角色id和信息id分页查询用户
     *
     * @param userId  用户id
     * @param pageIndex 页码
     * @param pageSize  每页显示数量
     * @return 用户分页对象
     */
    public PageHelper<User> showPageByOwnClazz( int userId, int pageIndex, int pageSize) throws Exception {
        return roleDetailInfoDao.showPageByOwnClazz(userId, pageIndex, pageSize);
    }

    /**
     * 根据用户角色id和用户id查询当前班级学生
     *
     * @param userId  用户id
     * @return 学生列表
     */
    public List<User> showByOwnClazz(int userId) throws Exception {
        return roleDetailInfoDao.showByOwnClazz(userId);
    }
}
